package UILayer;

import java.util.Scanner;
import CtrlLayer.*;
import ModelLayer.*;
/**
 * Write a description of class MainMenu here.
 * Ebal sam mu maikata na tva menu e! @Bacho
 * @author (Group A) 
 * @version (2014.11.03)
 */
public class ManagerUI
{
    private ProductController productContainer;
    private CustomerController customerContainer;

    public ManagerUI()
    {
        productContainer = new ProductController();
        customerContainer = new CustomerController();
    }

    public void managerMenuStart()
    {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeManagerMenu();

            switch (choise) 
            {
                case 1: startCreateProduct();
                break;
                case 2: startFindProduct();
                break;
                case 3: startUpdateProduct();
                break;
                case 4: startDeleteProduct();
                break;
                case 5: startOrderProduct();
                break;
                case 6: startUpdateCustomersDiscounts();
                break;
                case 7: startDeleteCustomer();
                break;
                case 8: exit = true;
                break;
            }
        }//end while
    }//end start

    private int writeManagerMenu()
    {
        int choise = 0;
        try
        {
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Manager Menu ***");
            System.out.println(" (1) Add Product");
            System.out.println(" (2) Find Product");
            System.out.println(" (3) Change Product Information");
            System.out.println(" (4) Delete Product");
            System.out.println(" (5) Order Product");
            System.out.println(" (6) Update Customer's discounts");
            System.out.println(" (7) Delete Customer");
            System.out.println(" (8) Back");
            System.out.print("\n Choose an option: ");

            choise = keyboard.nextInt();       
        } 
        catch(Exception e) 
        {
            System.out.println("ERROR!");
            pause();
            managerMenuStart();
        }
        return choise;
    }

    private void startCreateProduct()
    {
        Scanner keyboard = new Scanner(System.in);
        try 
        {
            System.out.println("Fill in the information of the new product: ");
            System.out.println("BarCode:");
            int barCode = Integer.parseInt(keyboard.nextLine());
            System.out.println("Product's Name:");
            String productName = keyboard.nextLine();
            System.out.println("Price:");
            double price = Double.parseDouble(keyboard.nextLine());
            System.out.println("Weight:"); 
            double weight = Double.parseDouble(keyboard.nextLine());
            System.out.println("Location:");
            String location = keyboard.nextLine(); 
            System.out.println("Quantity:");
            int quantity = keyboard.nextInt();

            Product product = new Product(barCode, productName, price, location, weight, quantity);
            if(productContainer.addProduct(product))
            {
                productContainer.addProduct(product);
                System.out.println("A new product has been added!");
            } 
            else 
            {
                System.out.println("A product with the same barcode already exists!");
            }
        } 
        catch (Exception e) 
        {
            System.out.println("Something went wrong!");
        }
        pause();
    }

    private void startFindProduct()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the product you are looking for:");
        try
        {
            int barCode = keyboard.nextInt();
            Product foundProduct = productContainer.findProductByBarCode(barCode);
            System.out.println("Barcode: " + foundProduct.getBarCode());
            System.out.println("Product's name: " + foundProduct.getProductName());
            System.out.println("Price: " + foundProduct.getPrice());
            System.out.println("Weight: " + foundProduct.getWeight());
            System.out.println("Location: " + foundProduct.getLocation());
            System.out.println("Quantity: " + foundProduct.getQuantity());

        } 
        catch (Exception e) 
        {
            System.out.println("The product you are looking for can not be found!");
        }
        pause();
    }

    private void startUpdateProduct()
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            System.out.println("Enter the barcode of the product:");
            int barCode = Integer.parseInt(keyboard.nextLine());
            System.out.println("Fill in the new information:");
            System.out.println("Product's name:");
            String name = keyboard.nextLine();
            System.out.println("Price:");
            double price = Double.parseDouble(keyboard.nextLine());
            System.out.println("Weight:");
            double weight = Double.parseDouble(keyboard.nextLine());
            System.out.println("Location:");
            String location = keyboard.nextLine();

            productContainer.updateProduct(barCode,name,price,weight,location);
            //productContainer.updateProduct(barCode,name,price,weight,location, productContainer.findProductByBarCode(barCode).increaseQuantity(quantity));
            System.out.println("The information has been updated!");
        }
        catch(Exception e) 
        {
            System.out.println("Error! Try again");
        }
        pause();
    }

    public void startDeleteProduct()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the barcode of the product you want to delete:");
            int barCode = keyboard.nextInt();
            productContainer.deleteProductByBarCode(barCode);    
        } 
        catch (Exception e) 
        {
            System.out.println("The product you are looking for can not be found!");
        }
        pause();
    }

    private void startOrderProduct()
    {
        Scanner keyboard = new Scanner(System.in);
        try
        {
            System.out.println("Enter the barcode of the product:");
            int barCode = Integer.parseInt(keyboard.nextLine());
            System.out.println("Enter how many would you like to buy:");
            int quantity = Integer.parseInt(keyboard.nextLine());
            if(productContainer.updateQuantity(barCode, quantity))
            {
                System.out.println("The information has been updated!");
            }
            else
            {
                System.out.println("You can't order a negative amount!");
            }
        }
        catch(Exception e) 
        {
            System.out.println("Error! Try again");
        }
        pause();
    }

    private void startUpdateCustomersDiscounts()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the ID of the customer:");
            int id = keyboard.nextInt();
            System.out.println("Current discount:" + customerContainer.findCustomerById(id).getDiscount() + "%");
            System.out.println("Enter the new discount (in %):");
            double discount = keyboard.nextDouble();
            customerContainer.findCustomerById(id).setDiscount(discount);
            System.out.println("The information was updated successfully!");
        } 
        catch (Exception e) 
        {
            System.out.println("The customer you are looking for can not be found!");
        }
        pause();
    }

    private void startDeleteCustomer()
    {
        try
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter the ID of the customer you want to delete:");
            int id = keyboard.nextInt();
            customerContainer.deleteCustomerById(id);    
        } 
        catch (Exception e) 
        {
            System.out.println("The customer you are looking for can not be found!");
        }
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }
}